package cn.afactory.tygz.ui;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.appupgrade.AppUpdateManager;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.views.AppSettingItem;

public class AppSettingsActivity extends BaseActivity {
    private AppSettingItem settings_black_list;
    private AppSettingItem settings_feedback;
    private AppSettingItem settings_app_update;
    private TextView settings_logout;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_settings);
        showBackView();
        initViews();
        initDatas();
        setTitleText(getString(R.string.label_title_settings));
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case AppUpdateManager.MSG_HIDE_UPATE_DLG:
                if (mAppUpdateManager != null) {
                    mAppUpdateManager.dismissProgressDialog();
                }
                if(msg.obj != null){
                    LocalUtils.showToast(this, (String) msg.obj);
                }
                break;
            case AppUpdateManager.MSG_HAS_UPDATE:
                if (mAppUpdateManager != null) {
                    mAppUpdateManager.dismissProgressDialog();
                    mAppUpdateManager.showUpdateInfoDialog(this);
                }
                break;
        }
    }

    private void initViews(){
        settings_black_list = (AppSettingItem)findViewById(R.id.settings_black_list);
        settings_black_list.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NaviUtils.gotoBlackListPage(AppSettingsActivity.this);
                }
            });

        settings_feedback = (AppSettingItem)findViewById(R.id.settings_feedback);
        settings_feedback.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    gotoFeedback();
                }
            });

        settings_app_update = (AppSettingItem)findViewById(R.id.settings_app_update);
        settings_app_update.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                handlerAppUpdate();
            }
        });

        settings_logout = (TextView)findViewById(R.id.settings_logout);
        settings_logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                doLogout();
            }
        });

    }

    private void initDatas(){
        settings_black_list.initItem(R.drawable.ic_setting_black_list, R.string.label_settings_black_list);
        settings_feedback.initItem(R.drawable.ic_setting_feedback, R.string.label_settings_feedback);
        settings_app_update.initItem(R.drawable.ic_setting_app_update, R.string.label_settings_app_update);
        settings_app_update.setSummary(LocalUtils.getVersionName(this));
    }

    /**
     * 跳转到以及那反馈的话题
     */
    public static final long FEEDBACK_TOPIC_ID = 999999;
    private void gotoFeedback(){
        SnsTopicInfo snsTopicInfo = new SnsTopicInfo();
        snsTopicInfo.topicId = FEEDBACK_TOPIC_ID;
        snsTopicInfo.topicName = getString(R.string.label_title_feed_back);
        snsTopicInfo.desc = "有怨言没怨言的都在这里吐槽!";
        NaviUtils.gotoTopicDetailActivity(this,snsTopicInfo);
    }

    /**
     * 手动检查更新
     */
    private void handlerAppUpdate(){
        checkUpdateApp();
    }

    /**
     * 退出账号
     */
    private void doLogout(){
        NetManager.getInstance(this).doLogout(new OnResponseListener<Boolean>() {
            @Override
            public void onInternError(int errorCode,
                                     String errorMessage) {
                if (this == null || errorMessage == null) {
                    LocalUtils.showToast(AppSettingsActivity.this, getString(R.string.toast_logout_failed));
                    return;
                }
                LocalUtils.showToast(AppSettingsActivity.this, errorMessage);
            }

            @Override
            public void onComplete(boolean isOK,
                                   Boolean isLogoutOk,
                                   int errorCode,
                                   String errorMsg) {
                if (isLogoutOk && isOK) {
                    LocalUtils.JumpToLogin(AppSettingsActivity.this);
                } else {
                    LocalUtils.showToast(AppSettingsActivity.this, errorMsg);
                }
            }
        });
    }

    private void checkUpdateApp() {
        checkUpdateApp(true);
    }

    AppUpdateManager mAppUpdateManager;
    private void checkUpdateApp(boolean isManual) {
        mAppUpdateManager = new AppUpdateManager(mUIHandler);
        mAppUpdateManager.checkUpdate(this, isManual);
    }
}
